import logging
import os

def setup_logger(filename : str =None):
    
    if filename:
        if os.path.exists(filename):
            os.remove(filename)
        file_handler = logging.FileHandler(filename=filename, encoding="utf-8")
    else:
        # if os.path.exists("app.log"):
        #     os.remove("app.log")
        # 创建 FileHandler 并指定 UTF-8 编码
        file_handler = logging.FileHandler(filename="app.log", encoding="utf-8")
    
    # 配置日志格式
    formatter = logging.Formatter(
        fmt="%(asctime)s - %(name)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s"
    )
    file_handler.setFormatter(formatter)
    
    # 配置日志记录器
    logging.basicConfig(
        level=logging.DEBUG,
        handlers=[file_handler]  # 使用自定义的 FileHandler
    )
    return logging.getLogger(__name__)


